/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package control;

import entity.xml.EMessaggio;
import entity.xml.EUtente;
import foundation.FMessaggi;
import java.awt.Dimension;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.jdom.JDOMException;
import presentation.proportional.gui;

/**
 *
 * @author Quirino
 */
public class CComponiMessaggio {
    
    private static CComponiMessaggio instance= null;
    
    private CComponiMessaggio(){
        
        
        
    }
    
    public static CComponiMessaggio getInstance(){
        
        if(instance==null) instance= new CComponiMessaggio();
        return instance;
        
    }
    
    public boolean sendMessage(ArrayList<EUtente> destinatari, String contenuto){
        
        boolean flag= false;
        
        EMessaggio message= new EMessaggio();
        
        message.setID(FMessaggi.getIstance().getNextID());
        message.setContenuto(contenuto);
        
        Calendar cal = Calendar.getInstance();
        message.setData(cal.getTime());
        message.setDestinatario(destinatari);
        message.setMittente(CBacheca.getIstance().getCurrUser());
        message.setIDConversazione(FMessaggi.getIstance().getNextIDConversation(CBacheca.getIstance().getCurrUser(), destinatari));
        if(message.getMittente().getID()==CBacheca.getIstance().getCurrUser().getID()) message.setStato(1);
        else message.setStato(1);
        
        try {
            flag= FMessaggi.getIstance().insertMessage(message);
        } catch (JDOMException ex) {
            Logger.getLogger(CComponiMessaggio.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(CComponiMessaggio.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        return flag;
        
    }
    
    public Dimension getDimensionFoglio(double coeffX, double coeffY){
        
        Dimension d= new Dimension();
        
        int w= Math.round((float) (this.getDimensionBacheca().width/coeffX));
        int h= Math.round((float) (this.getDimensionBacheca().height/coeffY));
        
        d.setSize(w, h);
        
        return d; 
        
    }
    
    public Dimension getDimensionBacheca(){
        
        Dimension d= new Dimension();
        
        int widthPanelBacheca= gui.getIstance().getPanelBachecaContainer().getWidth();
        int heightPanelBacheca= gui.getIstance().getPanelBachecaContainer().getHeight();
        
        d.setSize(widthPanelBacheca, heightPanelBacheca);
        
        return d;
        
    }
    
}
